
// 返回
	function back(dataObj){
		if(globalFun.GetQueryString('from')=='html'){
			window.history.go(-1)
			}else{
	        if (mark==1) {
	            setupWebViewJavascriptBridge(function(bridge){
	                bridge.callHandler('goBack',dataObj,function(resp){});
	              });
	        } else if (mark==0) {


	                //var result = lase;
	               window.stub.goBack();
	              //用接口stub, 通过调用内部类中的方法jsMethod给javas传回result。

	      		}
			}
	}

	$('.head-img').on('click',function(type,id,uuid,phone){
		var dataObj = {
			type:type,
			id:id,
			uuid:user_info.uuid,
			phone:user_info.phone
		}
		var dataObjT = JSON.stringify(dataObj);
		back(dataObjT);

	});

	if(Auth()){

	} else{
		toClientLogin(location.href)
	}

	var cartNum;

	var freightPrice = localStorage.getItem('freightPrice')
	console.log(freightPrice)

	var spId;

	// 点击返回跳转到商品详情页面
	$('.head-img').click(function(){
	    if(localStorage.getItem("comeform") == "center"){
	        localStorage.removeItem("comeform");
//	        sessionStorage.setItem('currentUrl',rootdomain+'html/singlecenter/gerencenter_login.html');
	            window.history.go(-1);
	    } else{
	            window.history.go(-1);
	    }
	})

	var cartid;
	var priceArr = [];
	// 查看购物车列表
	function Lookshopcart(){
		var dataObj = {
		  	uuid:user_info.uuid,
   			phone:user_info.phone
	 	}
	console.log(dataObj)

		 $.ajax({
		 	type:"post",
		 	url:apicom + "mobile/after/shopcart/cartlist",
//		 	url:"http://192.168.30.199:10001/mobile/after/shopcart/cartlist1",
		 	data:dataObj,
		 	async: false,
		 	dataType: "json",
		 	success:function(res){
		 		if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}
		 		if(res.code == 0){
		 			tips(res.msg)
		 		} else if(res.code == 1){
		 			console.log(res);
		 			console.log(res.content);
		 			if(!res.content){
		 				$('.nono').show();
		 				$('.foot').hide();
		 			} else{
		 				for(var i = 0; i < res.content.length; i++){
		 				cartNum = res.content.length;
		 				console.log(cartNum)
		 				if(res.content[i].address == "" || res.content[i].address == null){
		 					res.content[i].address = '../../img/b_dianpumoren.png'
		 				}
		 				$('<div class="main-box"><div class="main1"><div class="main1-a"><div class="allcheck"><input type="checkbox" class="checkbox" name="" ></div><div class="ltwen" service-id='+res.content[i].merchantId+'><p>'+res.content[i].merchantName+'</p></div><div class="back"><img src="../../img/d_cart3.png"></div></div></div></div>').appendTo($('.main'));
		 					 var resourse = res.content[i].lists;

		 					for(var j = 0; j < res.content[i].lists.length; j++){
		 						if(null==res.content[i].lists[j].address || undefined==res.content[i].lists[j].address || ''==res.content[i].lists[j].address){
									res.content[i].lists[j].address='../../img/d_mrt.png';
								}
		 						$('<div class="main2 shop'+j+'" data-price='+res.content[i].lists[j].salePrice+ ' data-psId='+res.content[i].lists[j].spId+' data-cartid='+res.content[i].lists[j].cartId+' data-num='+res.content[i].lists[j].buyNum+ ' data-kunum='+res.content[i].lists[j].kuNum+' data-zongjia= '+res.content[i].lists[j].salePrice*100*res.content[i].lists[j].buyNum/1+' sp-guige='+res.content[i].lists[j].specificationsid+'><div class="check"><input type="checkbox" class="checkbox" name="" ></div><div class="moreBig" data-id='+res.content[i].lists[j].id+' data-hunterId='+res.content[i].lists[j].hunterId+' data-merchantId='+res.content[i].merchantId+'><div class="sptp"><img src="'+res.content[i].lists[j].address+'"></div><div class="spwz"><p class="sp-mc">'+res.content[i].lists[j].name+'</p><p class="sp-color">'+res.content[i].lists[j].attribute+'</p><div class="spzj"><p class="maney">¥<span class="maney1">'+res.content[i].lists[j].salePrice+'</span></p><div class="spzj-you"><button class="jian">-</button><button class="num" value='+res.content[i].lists[j].buyNum+'>'+res.content[i].lists[j].buyNum+'</button><button class="jia">+</button></div></div></div></div></div>').appendTo($('.main-box').eq(i));

		 					 cartid = resourse[j].cartId;
		 					 console.log(cartid);
		 					}
		 					if($('.ltsl').html(undefined)){
		 						$('.ltsl').html()
		 					}
		 				}
		 			}
		 		}
		 	},
		 	error:function(err){
		 		console.log(err)
		 	}
		})
	}
	Lookshopcart()
//店铺全选  取消全选
var status=false;
$(document).on('click','.allcheck input',function(){
	var that=$(this);
	var child_input=$(this).parent().parent().parent().parent().find('.main2 input');
	$.each(child_input, function() {
		if(that.is(':checked')){
			$(this).prop('checked',true);
			$(this).parent().addClass('checks');
			that.parent().addClass('checks');

		}else{
			$(this).prop('checked',false);
			$(this).parent().removeClass('checks');
			that.parent().removeClass('checks');
		}
	});
	zongJia()
})
//单选取消全选
var len1=0;
var all_len=0;
var all_check=0;
$(document).on('click','.main2 input',function(){
	var inps=$(this).parents(".main-box").find('.main2 input');
	var len2=inps.length;
	$.each(inps, function(i,elem) {
		console.log(this)
		if($(elem).prop('checked')){
			$(elem).parent().addClass('checks');
			len1++;
		}else{
			$(elem).parent().removeClass('checks');
		}
	});
	zongJia()
	var per_inp=$(this).parents(".main-box").find('.main1 .allcheck input');
	if(len2==len1){
//		$(this).parent().parent().parent().find('.main1 .allcheck input').prop('checked',true);
		$(per_inp).prop('checked',true);
		$(per_inp).parent().addClass('checks');
		zongJia()
	}
	if(len2!=len1){
//		$(this).parent().parent().parent().find('.main1 .allcheck input').prop('checked',false);
		$(per_inp).prop('checked',false);
		$(per_inp).parent().removeClass('checks');
	}
	len1=0;
})
//全选
$(document).on('click','.quanxuan input',function(){
	var all_inp=$('.main-box input');
	var all_status=$(this).prop('checked');
	$.each(all_inp,function(){
		$(this).prop('checked',all_status);
		if(all_status){
			$(this).parent().addClass('checks');
		}else{
			$(this).parent().removeClass('checks');
		}
	})
	zongJia()
})
//单独input
$(document).on('click','input',function(){
	var all_inp=$('.main-box input');
	// console.log(all_inp)
	all_len=all_inp.length;
	if($(this).is(':checked')){
		$(this).parent().addClass('checks');
		zongJia()
	}else{
		$(this).parent().removeClass('checks');
	}
	$.each(all_inp,function(i,elem){
		if($(elem).prop('checked')){
			all_check++;
		}
	})
	if(all_check==all_len){
		$('.quanxuan input').prop('checked',true);
		$('input').parent().addClass('checks');
		zongJia()
	}else{
		$('.quanxuan').removeClass('checks')
		$('.quanxuan input').prop('checked',false);

	}
	all_check=0;
})


	 // 编辑购物车
	 // 减
	 	var dataObj = {
		 	phone:user_info.phone,
   			uuid:user_info.uuid
		 }
	 $('.jian').click(function(event){
		 event.stopPropagation();
	 	var num = $(this).next().html()/1;
	 	var price = $(this).parents('.main2').data('price')*100;
	 	var aaa = {}
	 	var that = $(this)
	 	num--
	 	if(num < 1){
	 		num = 1
			tips('该宝贝不能减少了呦~');
	 	}
	 	dataObj.buynum = num;
	 	dataObj.cartId = $(this).parents('.main2').data('cartid')
	 	$.ajax({
		 	type:"post",
		 	url:apicom + "mobile/after/shopcart/update",
		 	data:dataObj,
		 	success:function(res){
		 		if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}
		 		console.log(res);
		 		var sum = num*price
		 		console.log(sum)
		 		that.parents('.main2').data('zongjia',sum)
   				// aaa.sum = sum;
   				// console.log(aaa)
				if(num == 1){
					num = 1
				}
				zongJia()
		 	},
		 	error:function(err){
		 		console.log(err)
		 	}
		 })

	 	$(this).next().html(num)
	 	$(this).parents('.main2').data('num',num)

	 })

	 // 加
	 $('.jia').click(function(event){
		 event.stopPropagation();
	 	var num = $(this).prev().html()/1;
	 	var price = $(this).parents('.main2').data('price')*100;
	 	var aaa = {}
	 	var that = $(this)
	 	var bbb = []
	 	// var num = $(this).parents('.main2').data('num');
	 	// console.log(num)
	 	var kunum = $(this).parents('.main2').data('kunum');
	 	num++;
		var topNum = kunum;
		if(num > topNum){
			num = topNum;
			tips('亲，该宝贝不能购买更多哦')
		}
		dataObj.buynum = num;
	 	dataObj.cartId = $(this).parents('.main2').data('cartid')
	 	// var price = $(this).parents('.main2').data('price')/1;

	 	$(this).parents('.main2').data('num',num);
	 	$(this).prev().html(num)

		 $.ajax({
		 	type:"post",
		 	url:apicom + "mobile/after/shopcart/update",
		 	data:dataObj,
		 	success:function(res){
		 		if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}
		 		console.log(res);
				if(num == 1){
					num = 1
				}
		 		var sum = num*price
		 		console.log(sum)
		 		that.parents('.main2').data('zongjia',sum)
		 		zongJia()
		 	},
		 	error:function(err){
		 		console.log(err)
		 	}
		 })
	 })
//展示总价
		function zongJia(){
			var sss = $('.checks').parents('.main2')
			var zongjia=0
			console.log(sss)
			// var lalala = []
	 		for(var i =  0; i <sss.length; i++){
	 			// lalala.push(sss[i].getAttribute('data-zongjia')/1)
	 			 zongjia+=sss[i].getAttribute('data-zongjia')/1
	 			 console.log(sss[i].getAttribute('data-zongjia'))

	 		}
	 		var bbb = zongjia/100
	 		console.log(bbb)
		 	$('#allmaney').html(bbb)
		}

	function Sum() {
	 	var sum = 0;
	 	console.log(priceArr)
	 	if(priceArr.length > 0){
		 	for(var i = 0; i < priceArr.length; i++){
		 		sum= priceArr[i]
		 	}

		 	$('#allmaney').html(sum)
	 	} else {
		 	$('#allmaney').html('0')

	 	}
	 }



	 // 去结算
	$('.btn').click(function(){
		var id_list=[];
		//判断是否选中结算商品
		var checks_len=$('.checks').length;
		if(checks_len==0){
			tips('请选择结算商品');
		}else{
			var check_product=$('.checks').parent('.main2');
			$.e ach(check_product,function(i,e){
				id_list.push($(e).attr('data-cartid'));
			})
			id_list=id_list.join();

//			apicom + 'mobile/after/orderPlace/toPay'
			$.post(apicom + 'mobile/after/orderPlace/toPay',{
				id:user_info.id,
				shopCartIds:id_list,
				uuid:user_info.uuid,
				phone:user_info.phone
			},function(res){
				console.log(res)
				if(typeof(res) == 'string'){
					res = JSON.parse(res);
				}
				if(1==res.code){
					sessionStorage.setItem("shopCartIds",JSON.stringify(res.content));
					sessionStorage.setItem('zhuangt','2')
					window.location.href=rootdomain +'html/product-order/product-tjdd.html'
		 		}else{
		 			tips(res.msg)
		 		}
		 	})
		}
	})


	// 点击列表跳到商品详情页
	$(document).on('click','.moreBig',function(){
		var id = $(this).data('id');
		var hunterId = $(this).data('hunterid');
		var merchantId = $(this).data('merchantid');
		window.location.href = rootdomain +'html/shopDetail/shopDetail.html?id='+id+'&merchant_id='+merchantId+'&hunter_id='+hunterId

	})

	//点击名称 跳转服务团队页面
	$(document).on('click','.ltwen',function(){
		sessionStorage.setItem("fromHTML","H5");
		var id = $(this).attr('service-id');
		window.location.href = rootdomain +'html/service/serviceList.html?id='+id
	})





//删除购物车
$('#delete').on('click',function(){
	var fk = [];
    var ffkk = '';
	for(var i = 0;i<$('.check').length;i++){
		if($('.check').eq(i).hasClass('checks')){
			var sc = i;
       		fk.push($('.check').eq(sc).parents('.main2').data('psid') / 1)
		}
    }
	    if(fk.length == 1) {
	        ffkk = fk.join()
	    } else if(fk.length > 1) {
	        ffkk = fk.join(',')
	    }
		var dataObj = {
		    psIdList:ffkk,
		    id:498,
		    type:0
		}
            var checks_len=ffkk.length;
            console.log(checks_len)
            if(checks_len==0){
                tips('还未选择删除的商品');
            } else{
            	$.ajax({
	                type:"post",
	                      url:apicom+"mobile/after/shopcart/deleteShotCartByParams " ,
//	                url:'http://192.168.30.199:10001/mobile/after/shopcart/deleteShotCartByParams',
	                data:dataObj,
	                async: false,
	                dataType: "json",
	                success:function(res){
	                    console.log(res);
	                    if(typeof(res) == 'string'){
	                        res = JSON.parse(res);
	                    }

                    if(res.code == 1){
                  		window.location.reload();
                    } else{
                        tips(res.msg);
                    }
                },
                    error:function(err){
                        console.log(err);
                    }
                })
            }
})
